package com.config;
//
//import java.io.IOException;
//import java.io.PrintWriter;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//
//import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
//
///**
// * Created by Administrator on 2018/4/14.
// */
//public class SecurityInterceptor extends HandlerInterceptorAdapter {
//
//    public final static String SESSION_KEY_USER = "user";
//    public final static String SESSION_KEY_POST_Tikect = "ticket";//已经在用户手里的tickt
//    public final static String FILTER_METHOD = "POST";
//    public final static String TICKET_MSG ="ticket不能为空,请访问/portal/ticket获取有效的访问ticket";
//      /*
//      * 是否post成功，由前端判断，每次传ticket要比之前的高
//      * 1.记录用户ticket
//      * 2.若用户再传来同样的ticket则禁止(如成功则没事)
//      * 3.若不成功那必然不落库，则用户需要新提交一个ticket
//      * 4.要有按照ticket查询数据的接口，目前先不做
//      */
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//
////        System.out.println("url:"+request.getRequestURI()+" contextpath:"+request.getContextPath());
////        HttpSession session = request.getSession();
////        UserModel user = (UserModel) session.getAttribute(SESSION_KEY_USER) ;
////        if ( user != null) {
////            //已经登录
////            String method = request.getMethod();
////            if(FILTER_METHOD.equals(method)) {
////                Object userTicket = request.getParameter("ticket");
////                Response<Long> ret = new Response<Long>(false);
////                if( userTicket instanceof Long == false) {
////                    ret.setMsg("ticket格式有误,"+TICKET_MSG);//还要在提交之后清除这个post
////                    returnJson(response, JSON.toJSONString(ret));
////                    return false;
////                }
////                Long userTicketNo = (Long) userTicket;
////                Object serverTicket = session.getAttribute(SESSION_KEY_POST_Tikect) ;
////                if(serverTicket == null) {
////                    session.setAttribute(SESSION_KEY_POST_Tikect, userTicketNo);
////                    return true;
////                }
////                Long serverTicketNo = (Long) serverTicket;
////                if(userTicketNo <= serverTicketNo) {
////                    ret.setMsg("请求已过期,当前ticket为:"+serverTicketNo);//还要在提交之后清除这个post
////                    ret.setData(serverTicketNo);
////                    returnJson(response,JSON.toJSONString(ret));
////                    return false;
////                }
////                session.setAttribute(SESSION_KEY_POST_Tikect, userTicketNo);
////                return true;
////            }
////            return true;
////        }
////        Response<Void> ret = new Response<Void>(false);
////        ret.setMsg("请先进行登录!");
////        returnJson(response,JSON.toJSONString(ret));
////        return false;
//
//    	return true;
//
//
//    }
//
//
//    private void returnJson(HttpServletResponse response, String json) throws Exception{
//        PrintWriter writer = null;
//        response.setCharacterEncoding("UTF-8");
//        response.setContentType("application/json;charset=UTF-8");
//        try {
//            writer = response.getWriter();
//            writer.print(json);
//        } catch (IOException e) {
//
//        } finally {
//            if (writer != null) {
//                writer.close();
//            }
//
//        }
//    }
//}
